package cn.sc.allinpay.allfavour.business.common.popwindow;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * 
 * @description popupWindow
 * @author pany
 * @date 2017年3月22日下午5:09:58
 */
public abstract class PopWindow implements PopupWindow.OnDismissListener{

	private Activity activity;
	private PopupWindow popupWindow;

	public PopWindow(Activity activity) {
		this.activity = activity;
		popupWindow = new PopupWindow();
		final View view = LayoutInflater.from(activity).inflate(layoutId(), null);
		popupWindow.setContentView(view);
		popupWindow.setWidth(getWidth());//设置宽
		popupWindow.setHeight(getHeight());//设置高
		popupWindow.setBackgroundDrawable(new ColorDrawable(getBackgroundColor()));// 设置背景颜色
		popupWindow.setOutsideTouchable(isOutsideTouchable());// 设置可以触摸弹出框以外的区域
		popupWindow.setFocusable(isFocusable());// 设置可以获取焦点
		if (getAnimationStyle() > 0) {
			popupWindow.setAnimationStyle(getAnimationStyle());// 设置动画
		}

		// 在PopupWindow里面就加上下面代码，让键盘弹出时，不会挡住pop窗口。
		popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
		popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		popupWindow.setOnDismissListener(this);
		layoutView(view);
	}

	/**
	 * 设置视图id
	 *
	 * @return
	 */
	protected abstract int layoutId();

	/**
	 * 加载视图
	 *
	 * @return
	 */
	protected abstract void layoutView(View view);

	protected Activity getActivity() {
		return activity;
	}

	protected int getWidth() {
		return ViewGroup.LayoutParams.MATCH_PARENT;
	}

	protected int getHeight() {
		return ViewGroup.LayoutParams.WRAP_CONTENT;
	}

	/**
	 * 设置PopupWindow视图背景颜色
	 *
	 * @return
	 */
	protected int getBackgroundColor() {
		return 0xFFFFFFFF;
	}

	/**
	 * // 设置可以触摸弹出框以外的区域
	 *
	 * @return
	 */
	protected boolean isOutsideTouchable() {
		return true;
	}

	/**
	 * 设置可以获取焦点
	 *
	 * @return
	 */
	protected boolean isFocusable() {
		return true;
	}

	/**
	 * 设置动画
	 *
	 * @return
	 */
	protected int getAnimationStyle() {
		return -1;
	}

	/**
	 * 设置阴影部分的透明度
	 *
	 * @return
	 */
	protected float getWindowBackgroundAlpha() {
		return 0.5f;
	}

	/**
	 * 设置添加屏幕的背景透明度
	 *
	 * @param alpha
	 */
	private void setWindowAlpha(float alpha) {
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.alpha = alpha;
		activity.getWindow().setAttributes(lp);
	}

	/**
	 * 获取view对象便捷方法
	 *
	 * @param id 资源id
	 * @return
	 */
	protected <H extends View> H find(int id) {
		return (H) popupWindow.getContentView().findViewById(id);
	}

	/**
	 * 显示
	 *
	 * @param view
	 */
	public void show(View view) {
		setWindowAlpha(getWindowBackgroundAlpha());//设置屏幕透明度
		popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
	}

	/**
	 * 隐藏
	 */
	public void dismiss() {
		popupWindow.dismiss();
	}

	/**
	 * 获取屏幕高度
	 */
	public int getScreenHeight() {
		return getActivity().getResources().getDisplayMetrics().heightPixels;
	}

	@Override
	public void onDismiss() {
		// TODO Auto-generated method stub
		setWindowAlpha(1f);
	}
}
